var instdata = "";
var posdata = "";
function initial(){
    /**
    JSON.stringify()是将JavaScript对象转换为json字符串
    JSON.parse(jsonstr)是将json字符串转换为javascript对象
    */
    $.ajax({
        url:"http://localhost/PersonManSystem/inst/install.action",
        type:"get",
        dataType:'json',
        contentType:'application/json;charset=utf-8',
        //data:JSON.stringify(schoolBannerInfo),
        //data:schoolBannerInfo,//get方式
        //data:null,
        success:function(data){
            instdata = data;
            //alert(JSON.stringify(data));
            //alert(data['data']['一级机构'][0].instname);
            initLoadData('inst', data, '1');
            initLoadData('inst', data, '2');
            initLoadData('inst', data, '3');
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){
            alert("Error");
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus);
        }
    });

    $.ajax({
        url:"http://localhost/PersonManSystem/pos/all.action",
        type:"get",
        dataType:'json',
        contentType:'application/json;charset=utf-8',
        //data:JSON.stringify(schoolBannerInfo),
        //data:schoolBannerInfo,//get方式
        //data:null,
        success:function(data){
            posdata = data;
            //alert(JSON.stringify(data));
            //alert(data['data']['一级机构'][0].instname);
            initLoadData('pos', data, '1');
            initLoadData('pos', data, '2');
            initLoadData('pos', data, '3');
        },
        error:function(XMLHttpRequest, textStatus, errorThrown){
            alert("Error");
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus);
        }
    });
}

/*初始化机构*/
function initLoadData(arg, data, type){
    if (type == '1') {
        if (arg == 'inst') {
            var firstInstSelect = $("#firstinst");
            $(data['data']['一级机构']).each(function(){
                firstInstSelect.append("<option value='" + this.instid + "'>" + this.instname + "</option>");
            });
        } else {
            var firstpositionSelect = $("select[name = 'firstposition']");
            $(data['data']['职位分类']).each(function(){
                firstpositionSelect.append("<option value='" + this.posid + "'>" + this.posname + "</option>");
            });
        }
    }
    else if (type == '2') {
        if (arg == 'inst') {
            var secondInstSelect = $("#secondinst");
            var firstinstid = ($("#firstinst").val());
            $(data['data']['二级机构']).each(function(){
                if (this.instpid == firstinstid) {
                    secondInstSelect.append("<option value='" + this.instid + "'>" + this.instname + "</option>");
                }
            });
        } else {
            var secondpositionSelect = $("select[name = 'secondposition']");
            var firstposid = $("select[name = 'firstposition']").val();
            $(data['data']['职位名称']).each(function(){
                if (this.pospid == firstposid) {
                    secondpositionSelect.append("<option value='" + this.posid + "'>" + this.posname + "</option>");
                }
            });
        }
    } else {
        if (arg == 'inst') {
            var thirdInstSelect = $("#thirdinst");
            var secondinstid = ($("#secondinst").val());
            $(data['data']['三级机构']).each(function(){
                if (this.instpid == secondinstid) {
                    thirdInstSelect.append("<option value='" + this.instid + "'>" + this.instname + "</option>");
                }
            });
        } else {
            var thirdpositionSelect = $("select[name = 'thirdposition']");
            var secondposid = $("select[name = 'secondposition']").val();
            $(data['data']['职称']).each(function(){
                if (this.pospid == secondposid) {
                    thirdpositionSelect.append("<option value='" + this.posid + "'>" + this.posname + "</option>");
                }
            });
        }
    }
}

/*为页面加载点击添加事件*/
function loadBind() {
    /*为一级机构添加点击事件*/
    $("#firstinst").unbind("click");
    $("#firstinst").bind("click", function() {
        clickChange('inst', instdata, '1');
        clickChange('inst', instdata, '2');
        //alert($("select[name='secondinst']").find("option:selected").text());
    });
    /*为二级机构点击添加事件*/
    $("#secondinst").unbind("click");
    $("#secondinst").bind("click", function() {
        clickChange('inst', instdata, '2');
    });
    /*为职位分类添加鼠标点击事件*/
    $("select[name = 'firstposition']").unbind('click');
    $("select[name = 'firstposition']").bind('click', function(){
        clickChange('pos', posdata, '1');
        clickChange('pos', posdata, '2');
    });
    /*为职位名称添加鼠标点击事件*/
    $("select[name = 'secondposition']").unbind('click');
    $("select[name = 'secondposition']").bind('click', function(){
        clickChange('pos', posdata, '2');
    });
    /*为提交添加点击事件*/
    $("#formsubmit").unbind("click");
    $("#formsubmit").bind("click", function() {
        submitData();
    });
}

/*当上一级机构发生变化时，下一级机构跟着改变*/
function clickChange(arg, data, type){
    if (type == '1') {
        if (arg == 'inst') {
            var secondInstSelect = $("#secondinst");
            secondInstSelect.empty();
            var firstinstid = ($("#firstinst").val());
            $(data['data']['二级机构']).each(function(){
                if (this.instpid == firstinstid) {
                    secondInstSelect.append("<option value='" + this.instid + "'>" + this.instname + "</option>");
                }
            });
        } else {
            var secondpositionSelect = $("select[name = 'secondposition']");
            secondpositionSelect.empty();
            var firstposid = $("select[name = 'firstposition']").val();
            $(data['data']['职位名称']).each(function(){
                if (this.pospid == firstposid) {
                    secondpositionSelect.append("<option value='" + this.posid + "'>" + this.posname + "</option>");
                }
            });
        }
    }
    if (type == '2') {
        if (arg == 'inst') {
            var thirdInstSelect = $("#thirdinst");
            thirdInstSelect.empty();
            var secondinstid = ($("#secondinst").val());
            $(data['data']['三级机构']).each(function(){
                if (this.instpid == secondinstid) {
                    thirdInstSelect.append("<option value='" + this.instid + "'>" + this.instname + "</option>");
                }
            });
        } else {
            var thirdpositionSelect = $("select[name = 'thirdposition']");
            thirdpositionSelect.empty();
            var secondposid = $("select[name = 'secondposition']").val();
            $(data['data']['职称']).each(function(){
                if (this.pospid == secondposid) {
                    thirdpositionSelect.append("<option value='" + this.posid + "'>" + this.posname + "</option>");
                }
            });
        }
    }
}




function submitData(){

    sessionStorage.setItem("firstinstname", $("select[name='firstinst']").find("option:selected").text());
    sessionStorage.setItem("secondinstname", $("select[name='secondinst']").find("option:selected").text());
    sessionStorage.setItem("thirdinstname", $("select[name='thirdinst']").find("option:selected").text());
    sessionStorage.setItem("postype", $("select[name='firstposition']").find("option:selected").text());
    sessionStorage.setItem("posname", $("select[name='secondposition']").find("option:selected").text());
    sessionStorage.setItem("starttime", $("input[name='starttime']").val());
    sessionStorage.setItem("endtime", $("input[name='endtime']").val());

     //js对象
     window.location='RLDACX.html';

}